#include "ImguiLayer.h"
#include "../Application.h"
#include "hzpch.h"

// temporary
#include <glad/glad.h>
#include <GLFW/glfw3.h>

#define IMGUI_IMPL_API
#include "backends/imgui_impl_glfw.h"
#include "backends/imgui_impl_opengl3.h"
#include "backends/imgui_impl_opengl3_loader.h"

namespace Hazel
{
ImguiLayer::ImguiLayer() : Layer("ImguiLayer")
{
    // std::cout<<"ImguiLayer\n";
    // getchar();
}
ImguiLayer::~ImguiLayer()
{
}
void ImguiLayer::OnAttach()
{
    HZ_PROFILE_FUNC();
    // std::cout<<"__func__"<<'\n';
    // getchar();
    // GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+OpenGL3 example", nullptr, nullptr);
    IMGUI_CHECKVERSION();

    // GLFWwindow* window=glfwGetCurrentContext();
    // if (window == nullptr)
    //     return;
    // glfwMakeContextCurrent(window);
    ImGui::CreateContext();
    ImGui::StyleColorsDark();

    ImGuiIO &io = ImGui::GetIO();
    (void)io;
    io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
    // io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;      // Enable Gamepad Controls
    io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;   // Enable Docking
    io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows

    ImGuiStyle &style = ImGui::GetStyle();
    if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
    {
        style.WindowRounding = 0.0f;
        style.Colors[ImGuiCol_WindowBg].w = 1.0f;
    }

    Application &app = Application::Get();
    GLFWwindow *window = static_cast<GLFWwindow *>(app.GetWindow().GetNativeWindow());

    ImGui_ImplGlfw_InitForOpenGL(window, true);
    ImGui_ImplOpenGL3_Init("#version 410");

    // ImGui_ImplGlfw_InitForOpenGL
}
void ImguiLayer::OnDetach()
{
    HZ_PROFILE_FUNC();

    ImGui_ImplOpenGL3_Shutdown();
    ImGui_ImplGlfw_Shutdown();
    ImGui::DestroyContext();
}
// void ImguiLayer::OnUpdate()
// {

//     ImGuiIO& io=ImGui::GetIO();
//     Application& app=Application::Get();
//     io.DisplaySize=ImVec2(app.GetWindow().GetWidth(),app.GetWindow().GetHeight());

//     float time=(float)glfwGetTime();
//     io.DeltaTime= m_Time> 0.0f?(time-m_Time) : (1.0f/60.0f);
//     m_Time=time;

//     ImGui_ImplOpenGL3_NewFrame();
//     ImGui_ImplGlfw_NewFrame();
//     ImGui::NewFrame();

//     static bool show = true;
//     ImGui::ShowDemoWindow(&show);
//     ImGui::Render();
//     ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// }
void ImguiLayer::Begin()
{
    HZ_PROFILE_FUNC();

    ImGui_ImplOpenGL3_NewFrame();
    ImGui_ImplGlfw_NewFrame();
    ImGui::NewFrame();
}
void ImguiLayer::End()
{
    HZ_PROFILE_FUNC();

    ImGuiIO &io = ImGui::GetIO();
    Application &app = Application::Get();
    io.DisplaySize = ImVec2(app.GetWindow().GetWidth(), app.GetWindow().GetHeight());

    ImGui::Render();
    ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

    if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
    {
        GLFWwindow *backup_current_context = glfwGetCurrentContext();
        ImGui::UpdatePlatformWindows();
        ImGui::RenderPlatformWindowsDefault();
        glfwMakeContextCurrent(backup_current_context);
    }
}
} // namespace Hazel